﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;

namespace WebApplication2
{
    /// <summary>
    /// Summary description for FetchCustomer1
    /// </summary>
    public class FetchCustomer1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            string connect = @"Server=USER-PC\SQLEXPRESS;Database=Northwind;Trusted_Connection=True";
            string query = "SELECT CompanyName, Address, City, Region, PostalCode," +
                      "Country, Phone, Fax FROM Customers WHERE CustomerID = @CustomerID";
            string id = context.Request.QueryString["CustomerID"];
            if (id != null && id.Length == 5)
            {
                using (SqlConnection conn = new SqlConnection(connect))
                {
                    using (SqlCommand cmd = new SqlCommand(query, conn))
                    {
                        cmd.Parameters.AddWithValue("CustomerID", context.Request.QueryString["CustomerID"]);
                        conn.Open();
                        SqlDataReader rdr = cmd.ExecuteReader();
                        if (rdr.HasRows)
                        {
                            while (rdr.Read())
                            {
                                context.Response.Write("<p>");
                                context.Response.Write("<strong>" + rdr["CompanyName"].ToString() + "</strong><br />");
                                context.Response.Write(rdr["Address"].ToString() + "<br />");
                                context.Response.Write(rdr["City"].ToString() + "<br />");
                                context.Response.Write(rdr["Region"].ToString() + "<br />");
                                context.Response.Write(rdr["PostalCode"].ToString() + "<br />");
                                context.Response.Write(rdr["Country"].ToString() + "<br />");
                                context.Response.Write("Phone: " + rdr["Phone"].ToString() + "<br />");
                                context.Response.Write("Fax: " + rdr["Fax"].ToString() + "</p>");
                            }
                        }
                    }
                }
            }
            else
            {
                context.Response.Write("<p>No customer selected</p>");
            }
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}